package com.springboot.ww.config.context;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.util.StringUtils;

/**
 * 租户上下文.
 *
 * @author yangruibo
 */
@Slf4j
public class TenantContext {
    private TenantContext() {}

    /**
     * The constant KEY_TID.
     */
    public static final String KEY_TID = "tenantId";
    public static final String KEY_TOKEN = "Blade-Auth";

    /**
     * 获取租户ID.
     */
    public static String getTID() {
        String tid = ThreadLocalUtil.get(KEY_TID);
        if (!StringUtils.isEmpty(tid)) {
            return tid;
        }
        if (!StringUtils.isEmpty(tid) && !"opms".equals(tid)) {
            bind(tid);
            return tid;
        }
        return null;
    }

    /**
     * 绑定.
     */
    public static void bind(String tid) {
        ThreadLocalUtil.put(KEY_TID, tid);
    }

    /**
     * 解绑.
     */
    public static String unbind() {
        String tid = ThreadLocalUtil.getAndRemove(KEY_TID);
        return tid;
    }
}
